import { createSlice } from '@reduxjs/toolkit'

// 持久化数据
let tabIdx = Number(localStorage.getItem('tabIdx')) || 0
let userInfo = localStorage.getItem('userInfo')
userInfo &&= JSON.parse(userInfo)


export const initialState = {
  tabIdx,
  count: 0,
  isMobile: false,
  userInfo: userInfo
}


export const countSlice = createSlice({

  name: 'countSpace', // 命名空间,在调用action的时候会默认的设置为action的前缀,保证唯一.不重名

  initialState,

  reducers: {
    // reducer函数 state当前组件的数据 
    //第二个参数为{payload:{},type:"""} 想想vuex写法
    increment:(state) => {
      state.count += 1
    },
    // 切换菜单
    changIdx:(state,{payload}) => {
      state.tabIdx = payload
      localStorage.setItem('tabIdx',payload)
    },
    decrement:(state,action) => {
      state.count -= action.payload
    },
    updateUserInfo:(state,{payload}) => {
      state.userInfo = payload
    },
    checkIsMobile:(state,{payload}) => {
      state.isMobile = payload
    }
  }



})











export const { increment, decrement, updateUserInfo, checkIsMobile, changIdx }  = countSlice.actions

export const selectCounter = (state) => state.counter;



export default countSlice.reducer